package com.project.controller.article;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.hibernate.criterion.Order;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.project.controller.BaseController;
import com.project.entity.article.Article;
import com.project.entity.article.ArticleLog;
import com.project.entity.enumcommon.PublishStatusEnum;
import com.project.service.article.ArticleLogService;
import com.project.service.article.ArticleService;
import com.project.service.article.CategoryService;
import com.project.util.CurrentSessionUtil;

@Controller
@RequestMapping("/article")
public class ArticleController extends BaseController<Article>{
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private ArticleLogService articleLogService;
	
	@Override
	protected Class<Article> getClazz() {
		return Article.class;
	}
	
	@RequestMapping("list")
	public String list(Model model,@ModelAttribute Article article) {
		model.addAttribute("entity", article);
		article.setcOrder(Order.desc("id"));
		List<Article> list = service.query(article);
		model.addAttribute("list", list);
		model.addAttribute("count", list.size());
		return "/article/list";
	}
	
	@RequestMapping("form")
	public String from(Model model,@RequestParam(required=false) Long id) {
	    model.addAttribute("labelMaps", null);
		model.addAttribute("categoryMaps", categoryService.findCategoryList());
		model.addAttribute("labelMaps", null);
		if(id!=null){
		    ArticleLog log = new ArticleLog();
		    log.setcOrder(Order.desc("id"));
		    log.setArticleId(id);
		    model.addAttribute("list", articleLogService.query(log));
            model.addAttribute("entity", getEntity());
            model.addAttribute("labelMaps", categoryService.findLabelList(getEntity().getCategoryId()));
        }
		return "/article/form";
	}
	
	@RequestMapping("getLabelMap")
    @ResponseBody
    public Map<String,String> getLabelMap(@RequestParam(required=false,value="categoryId") Long categoryId){
        if(StringUtils.isEmpty(categoryId)){
            return null;
        }
        
        return categoryService.findLabelList(categoryId);
    }
	@RequestMapping("save")
	@ResponseBody
    public Long save(Model model,@ModelAttribute(name="entity") Article article) {
	    if(article.getId()==null){
	        article.setPublishStatus(PublishStatusEnum.N);
	        service.save(article);
	    }else{
	    	service.update(article);
	        //保存修改日志
	        ArticleLog log = new ArticleLog();
            log.setArticleId(article.getId());
            log.setCreateTime(new Date());
            log.setCreateUser(CurrentSessionUtil.getCurrentUser().getUserCode());
            articleLogService.save(log);
	    }
        return article.getId();
    }
	
	@RequestMapping("view")
    public String view(Model model,@RequestParam(required=true) Long id) {
	    model.addAttribute("entity", getEntity());
        
        return "/article/view";
    }

	
	@RequestMapping("publish")
	@ResponseBody
    public String publish(Model model) {
	    Article article = getEntity();
	    if(article.getDr()==1){
			return "该文章已删除";
		}
	    article.setPublishStatus(PublishStatusEnum.P);
	    article.setPublishTime(new Date());
	    service.update(article);
        return "true";
    }
	/**
	 * 逻辑删除
	 * @return
	 */
	@RequestMapping("delete")
	@ResponseBody
	public String delete(){
		Article article = getEntity();
	    article.setDr(1);
	    service.update(article);
		return "true";
	}
	/**
	 * 取消发布
	 * @return
	 */
	@RequestMapping("cancelPublish")
	@ResponseBody
	public String cancelPublish(){
		Article article = getEntity();
		if(article.getDr()==1){
			return "该文章已删除";
		}
		if(!PublishStatusEnum.P.equals(article.getPublishStatus())){
			return "该文章未发布";
		}
		article.setPublishStatus(PublishStatusEnum.N);
	    service.update(article);
		return "true";
	}
	
	
}
